/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Vector;

import javax.swing.ImageIcon;


/**
 * IconPool
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class IconPool {
  
  private static IconPool instance = new IconPool();
  
  public static final String DEFAULT_CONTAINER_ID = "Folder.gif";
  public static final String DEFAULT_ELEMENT_ID = "Document.gif";
  public static final String DEFAULT_TAN_ID = "Lock.gif";
  
  private static final String ICON_PACKAGE = "/org/javalobby/icons/20x20/";
  
  private SortedMap icons;
  
  /**
   * Constructor for IconPool (Signleton)
   */
  private IconPool() {
    // fill Map
    icons = new TreeMap();
    // read icons from source package
    InputStream input = getClass().getResourceAsStream(ICON_PACKAGE + "icons.sorted");
    LineNumberReader lineReader = new LineNumberReader(new InputStreamReader(input));
    String line;
    try {
      while ((line = lineReader.readLine()) != null) {
        icons.put(line, new ImageIcon(IconPool.class.getResource(ICON_PACKAGE + line)));
      }
    } catch (IOException e) {
      // nothing
      System.out.println("Failed to read Icon Pool");
    }
  }
  
  /**
   * getInstance
   *
   * @return
   */
  public static IconPool getInstance() {
    return instance;
  }
  
  /**
   * getIconForId
   *
   * @param name
   * @return
   */
  public ImageIcon getIconForId(String name) {
    return (ImageIcon)icons.get(name);
  }
  
  /**
   * getAllIds
   *
   * @return
   */
  public Vector getAllIds() {
    return new Vector(icons.keySet());
  }

}
